const router = require('koa-router')()
const Http = require('../libs/Http');
const axios = require('axios');
const got = require('got');

router.get('/code2Session', async (ctx) => {
  let code = ctx.request.query.code;
  let response =await Http.get('https://api.weixin.qq.com/sns/jscode2session',{
    appid:'wxd817f7e236de4d07',
    secret:'c1a03e0551cb893b077b9a34513881e9',
    js_code:code,
    grant_type:'authorization_code',
  })
  
  ctx.body = response;
})

router.post('/getPhoneNumber', async (ctx) => {
  let code = ctx.request.body.code;
  let access_token = ctx.request.body.access_token;
  console.log(ctx.header);
  console.log('getPhoneNumber',code,access_token);
  let response = await Http.postBody(`https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=${access_token}`,{
    code,
  })
  ctx.body = response;
})

router.get('/getAccessToken', async (ctx) => {
  let response =await Http.get('https://api.weixin.qq.com/cgi-bin/token',{
    appid:'wxd817f7e236de4d07',
    secret:'c1a03e0551cb893b077b9a34513881e9',
    grant_type:'client_credential',
  })
  
  ctx.body = response;
})

module.exports = router;